package com.hxm.viewdraghelper;

import android.content.Context;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

/**
 * Created by Hxm on 2017/2/27.
 */

public class DragLayout extends LinearLayout {

    private View viewOne, viewTwo;
    private ViewDragHelper dragHelper;

    public DragLayout(Context context) {
        this(context, null);
    }

    public DragLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DragLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        dragHelper = ViewDragHelper.create(this, 1.0f, new DragHelperCallback());
        dragHelper.setEdgeTrackingEnabled(ViewDragHelper.EDGE_ALL);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return dragHelper.shouldInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        dragHelper.processTouchEvent(event);
        return true;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        viewOne = getChildAt(0);
        viewTwo = getChildAt(1);
    }

    private void showToast(String text) {
        Toast.makeText(getContext(), text, Toast.LENGTH_SHORT).show();
    }

    private class DragHelperCallback extends ViewDragHelper.Callback {

        //拦截可以操作的view,直接return true,表示所有子View都可以进行拖拽
        @Override
        public boolean tryCaptureView(View child, int pointerId) {
            return true;
        }

        //水平滑动
        @Override
        public int clampViewPositionHorizontal(View child, int left, int dx) {
            int leftBound = getPaddingLeft();
            int rightBound = getWidth() - child.getWidth() - getPaddingRight();
            return Math.min(Math.max(left, leftBound), rightBound);
        }

        //垂直滑动
        @Override
        public int clampViewPositionVertical(View child, int top, int dy) {
            int topBound = getPaddingTop();
            int bottomBound = getHeight() - child.getHeight() - getPaddingBottom();
            return Math.min(Math.max(top, topBound), bottomBound);
        }

        @Override
        public void onEdgeTouched(int edgeFlags, int pointerId) {
//            switch (edgeFlags){
//                case ViewDragHelper.EDGE_LEFT:
//                    showToast("触碰左边缘");
//                    break;
//                case ViewDragHelper.EDGE_RIGHT:
//                    showToast("触碰右边缘");
//                    break;
//                case ViewDragHelper.EDGE_TOP:
//                    showToast("触碰上边缘");
//                    break;
//                case ViewDragHelper.EDGE_BOTTOM:
//                    showToast("触碰下边缘");
//                    break;
//            }
            showToast("触摸边缘");
        }

        @Override
        public void onEdgeDragStarted(int edgeFlags, int pointerId) {
            dragHelper.captureChildView(viewTwo, pointerId);
        }

        @Override
        public void onViewReleased(View releasedChild, float xvel, float yvel) {
            super.onViewReleased(releasedChild, xvel, yvel);
        }

        @Override
        public void onViewCaptured(View capturedChild, int activePointerId) {
            super.onViewCaptured(capturedChild, activePointerId);
        }
    }
}
